/**
 * Copyright (c) 2023 murenchao
 * taomu is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *       http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package cool.taomu.toolkit.jersey.tomcat

import com.google.inject.Inject
import com.google.inject.name.Named
import com.google.inject.servlet.GuiceFilter
import com.google.inject.servlet.GuiceServletContextListener
import cool.taomu.toolkit.Context
import java.io.File
import javax.inject.Provider
import org.apache.catalina.LifecycleException
import org.apache.catalina.Server
import org.apache.catalina.startup.Tomcat
import org.apache.tomcat.util.descriptor.web.FilterDef
import org.apache.tomcat.util.descriptor.web.FilterMap

import static cool.taomu.toolkit.jersey.tomcat.TomcatProvider.A.*

class TomcatProvider implements Provider<Server> {

	@Inject
	@Named("resourceConfig")
	String resourceConfig;

	@Inject(optional=true)
	@Named("taomu.http.name")
	String name;

	@Inject(optional=true)
	@Named("taomu.http.host")
	String host = "0.0.0.0";

	@Inject(optional=true)
	@Named("taomu.http.port")
	int port = 8080;

	@Inject(optional=true)
	@Named("taomu.http.path")
	String contextPath = "/";

	@Inject(optional=true)
	@Named("taomu.http.static.path")
	String staticBasePath = null;

	@Inject(optional=true)
	@Named("taomu.http.pathSpec")
	String pathSpec = "";

	val tomcat = new Tomcat();

	@Inject
	Context supplier;
	
	static class A extends GuiceServletContextListener{
		new(){}
		public static Context supplier;	
		override protected getInjector() {
			return supplier.injectorSupplier.get();
		}
		
	}

	override get() {
		if (!contextPath.startsWith("/")) {
			contextPath = "/" + contextPath;
		}
		if (name === null || name.equals("")) {
			name = String.format("%s-%s", host, port);
		}
		Runtime.getRuntime().addShutdownHook(
			new Thread([
				try {
					tomcat.destroy();
				} catch (LifecycleException e) {
					e.printStackTrace();
				}
			])
		);
		tomcat.hostname = host;
		tomcat.port = port;
		tomcat.baseDir = "./src/main"
		tomcat.connector;
		var File base = new File(System.getProperty("java.io.tmpdir"));
		var ctx = tomcat.addWebapp("/",".");
		//tomcat.addContext("/", base.absolutePath);
		ctx.setReloadable(false);
		var filter = new FilterDef();
		filter.filterName = "guiceFilter" ;
		filter.filterClass = GuiceFilter.name;
		ctx.addFilterDef(filter);
		var filterMap = new FilterMap();
		filterMap.addURLPattern("/*");
		filterMap.filterName = "guiceFilter";
		ctx.addFilterMap(filterMap);
		filter.addInitParameter("javax.ws.rs.Application", resourceConfig);
		A.supplier = supplier;
		ctx.addApplicationListener(A.name)
		var wrapper = Tomcat.addServlet(ctx, "base", "org.glassfish.jersey.servlet.ServletContainer");
	 	wrapper.addMapping("/*");
	 	wrapper.addInitParameter("javax.ws.rs.Application", resourceConfig);
		tomcat.start();
		tomcat.server
	}

}
